function split( val ) {
    return val.split( /,\s*/ );
}

function extractLast( term ) {
    return split( term ).pop();
}

$(function(){
    
    var modal = $('<div />').addClass('modal');
    var mask = $('<div />').addClass('mask');
    var content = $('<div />').addClass('content');

    modal.appendTo($('body'));
    mask.appendTo(modal);
    content.appendTo(modal);
    
    modal.pop = function(){
        modal.show();
        content.css('top', '80px');
        content.css('left', ($(window).width() - content.width())/2 + 'px');
    };
    
    mask.click(function(){
        modal.hide();
    });
    
    $('.add-procedimento').click(function(e){
        e.preventDefault();
        var idFluxo = $(this).attr('href');
        content.html('');
        $.ajax({
            url : BASEURL + 'chamado/addproc/' + idFluxo,
            success : function(data){
                content.append(data);
                $('#ics', content).bind("keydown", function(event) {
                    if (event.keyCode === $.ui.keyCode.TAB && $(this).data("ui-autocomplete").menu.active) {
                        event.preventDefault();
                    }
                }).autocomplete({
                    minLength: 2,
                    source: function( request, response ) {
                        // delegate back to autocomplete, but extract the last term
                        response( $.ui.autocomplete.filter(
                            split($('#dbics', content).val()), extractLast( request.term ) ) );
                    },
                    focus: function() {
                        // prevent value inserted on focus
                        return false;
                    },
                    select: function( event, ui ) {
                      var terms = split( this.value );
                      // remove the current input
                      terms.pop();
                      // add the selected item
                      terms.push( ui.item.value.split( /-\s*/ )[0] );
                      // add placeholder to get the comma-and-space at the end
                      terms.push( "" );
                      this.value = terms.join( ", " );
                      return false;
                    }
                });
            }
        });
        modal.pop();
        return false;
    });
    
    $(window).resize(function(){
        content.css('top', '80px');
        content.css('left', ($(window).width() - content.width())/2 + 'px');
    });
    
    $('.confirm').click(function(e){
        e.preventDefault();
        
        var el = $(this);
        
        var text = $('input', this).val();
        content.html('<p>Tem certeza que deseja ' + text.toLowerCase() + ' este chamado?</p>')
        
        var confirm = $('<input type="submit" value="Confirmar" />');
        var cancel = $('<input type="submit" value="Cancelar" />');
        
        confirm.css('float', 'right');
        
        var div = $('<div />');
        div.append(confirm);
        div.append(cancel);
        
        content.append(div);
        
        confirm.click(function(){
            window.location.href = el.attr('href');
        });
        
        cancel.click(function(){
            modal.hide();
        });
        
        modal.pop();
        return false;
    });
    
});